package com.itlaobing.annotation.advice;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;

import java.time.LocalDateTime;
import java.util.logging.Logger;

public class TimeAdvice {

    private Logger logger = Logger.getLogger( this.getClass().getName() );

    public void before( JoinPoint point ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "]");
    }

    public Object around( ProceedingJoinPoint point ) throws Throwable {
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [ before: " + name + "]");
        Object result = point.proceed() ;
        logger.info( LocalDateTime.now().toString() + " [ after : " + name + "]");
        return result ;
    }

    public void afterThrow( JoinPoint point , Throwable th ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "] : " + th.toString());
    }

    public void afterReturn( JoinPoint point , Object rt ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "] : " + rt );
    }

    public void after( JoinPoint point ){
        String name = point.getSignature().getName() ;
        logger.info( LocalDateTime.now().toString() + " [" + name + "]");
    }

}
